Skip to main content
  1. SwiftUI in 100 Days Notes/

Day 4 - What is Swift Type Annotation? Why Do We Need It? When Do We Use It?

Swift can assign the type of the constant (let) or variable (var) we create based on the initial value we give it. However, in some cases we may not want to assign this value immediately or let Swift assign the type for us.

let surname = "Lasso"
var score = 0

In the example above, type inference can be understood. According to Type inference, surname variable is String and score variable is Int.

The Type annotations allow us to explicitly specify which data type we want and can be used as follows.

let surname: String = "Lasso"
var score: Int = 0

In some cases we may want to specify the data type of the variable ourselves.

var score: Double = 0

If we had not included :Double in the code above, Swift would have set the type of the score variable to Int.

Let’s use the types we’ve learned so far to define variables using type annotations.

String #

let playerName: String = "Roy"

Int #

var luckyNumber: Int = 13

Double #

let pi: Double = 3.141

Bool #

var isAuthenticated: Bool = true

Array #

The type of Array is specified by writing [String].

var albums: [String] = ["Red", "Fearless"]

Dictionary #

Dictionary is specified to have a Key and Value type of [String: String].

var user: [String: String] = ["id": "@twostraws"]

Set #

var books: Set<String> = Set(["The Bluest Eye", "Foundation", "Girl, Woman, Other"])

Why Do We Need Type Annotation? #

The most common use of type annotation is with constants (let) that we have not yet assigned a value to.

let username: String
// some complex code exists in this range.
username = "@twostraws"
// some complex code exists in this range.
print(username)

We say that username will contain a String anywhere in the code. Later in the code, we assign a value to the constant username. Later in the code, we use print to use the value of the constant username. Of course, not with such simple code in real projects.

If we had tried to use print(username) before assigning the value to username, Xcode would have given us an error. Also, since we assigned username once, if we tried to assign it a second time, Xcode would warn us about it again.

For this type of code we need a type annotation, because without an initialization Swift doesn’t know what kind of data username will contain.

When Do We Need Type Annotation? #

  1. When Swift doesn’t know which type to use.

    This usually happens in more advanced code. For example, let’s say we are downloading data from the internet, by default Swift cannot specify the type of this data, so we have to specify the type.

  2. When we don’t want to use the type Swift assigns by default.

    For example, when we want to use a Double number by assigning an integer, we use the type annotation.

    var percentage: Double = 99
    

    In the code above, the value of percentage is 99.0 even though we assign the value 99 as an integer.

  3. When we don’t want to assign a value yet.

    We use the type annotation when we know that the variable will exist, but we don’t want to set its value yet.

You can also read this article in Turkish.
Bu yazıyı Türkçe olarak da okuyabilirsiniz.

This article contains the notes I took for myself from the articles found at SwiftUI Day 4. Please use the link to follow the original lesson.